﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace CarAppClient.Helpers
{
    /// <summary>
    /// 颜色处理
    /// </summary>
    public static class ColorHelper
    {
        /// <summary>
        /// Offset 转Color
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static Color GetColorByOffset(GradientStopCollection collection, double offset)
        {
            var stops = collection.OrderBy(x => x.Offset).ToArray();
            if (offset <= 0) return stops[0].Color;
            if (offset >= 1) return stops[stops.Length - 1].Color;
            var left = stops.Where(s => s.Offset <= offset).Last();
            var right = stops.Where(s => s.Offset > offset).First();
            offset = Math.Round((offset - left.Offset) / (right.Offset - left.Offset), 2);
            var a = (byte)((right.Color.A - left.Color.A) * offset + left.Color.A);
            var r = (byte)((right.Color.R - left.Color.R) * offset + left.Color.R);
            var g = (byte)((right.Color.G - left.Color.G) * offset + left.Color.G);
            var b = (byte)((right.Color.B - left.Color.B) * offset + left.Color.B);
            return Color.FromArgb(a, r, g, b);
        }

        /// <summary>
        /// String转换成Color
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static Color ToColor(this string color)
        {
            return (Color)ColorConverter.ConvertFromString(color);
        }

        /// <summary>
        /// String转换成Brush
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static Brush? ToBrush(this string color)
        {
            BrushConverter brushConverter = new BrushConverter();
            var brush = brushConverter.ConvertFromString(color);
            if (brush!=null)
            {
                return (Brush)brush;
            }
            return null;
        }

        /// <summary>
        /// Color转换成Brush
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static SolidColorBrush ToBrush(this Color? color)
        {
            if (color == null)
                return new SolidColorBrush(Colors.Transparent);
            return new SolidColorBrush((Color)color);
        }

        /// <summary>
        /// Brush转Color
        /// </summary>
        /// <param name="brush"></param>
        /// <returns></returns>
        public static Color ToColor(this Brush brush)
        {
            if (brush == null)
            {
                return Colors.Transparent;
            }
            if (brush is SolidColorBrush)
            {
                var solidColorBrush = (brush as SolidColorBrush);
                if (solidColorBrush != null)
                {
                    return solidColorBrush.Color;
                }
            }
            else if (brush is LinearGradientBrush)
            {
                var linearGradientBrush = (brush as LinearGradientBrush);
                if (linearGradientBrush != null)
                {
                    return linearGradientBrush.GradientStops[0].Color;
                }
            }
            else if (brush is RadialGradientBrush)
            {
                var radialGradientBrush = (brush as RadialGradientBrush);
                if (radialGradientBrush != null)
                {
                    return radialGradientBrush.GradientStops[0].Color;
                }
            }

            return Colors.Transparent;
        }

        /// <summary>
        /// SolidColorBrush转Color
        /// </summary>
        /// <param name="brush"></param>
        /// <returns></returns>
        public static Color ToColor(this SolidColorBrush brush)
        {
            if (brush == null)
                return Colors.Transparent;
            return brush.Color;
        }

        /// <summary>
        /// Color转换成SolidColorBrush
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static SolidColorBrush ToBrush(this Color color)
        {
            var brush = new SolidColorBrush(color);
            brush.Freeze();
            return brush;
        }

        public static string ToHexString(this Color color, bool withAlpha = true)
        {
            if (withAlpha)
                return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.A, color.R, color.G, color.B);
            else
                return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
        }

        public static string ToHexString(this Color? color, bool withAlpha = true)
        {
            if (color == null)
                return "";

            var realColor = (Color)color;
            if (withAlpha)
                return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", realColor.A, realColor.R, realColor.G, realColor.B);
            else
                return string.Format("#{0:X2}{1:X2}{2:X2}", realColor.R, realColor.G, realColor.B);
        }

        public static LinearGradientBrush ToBrush(this Color[] colors)
        {
            var lcb = new LinearGradientBrush();
            for (int i = 0; i < colors.Length; i++)
            {
                lcb.GradientStops.Add(new GradientStop()
                {
                    Color = colors[i],
                    Offset = i * 1.0 / colors.Length
                });
            }
            return lcb;
        }

        public static bool IsEqual(this Color thisColor, Color color)
        {
            return thisColor.A == color.A && thisColor.R == color.R && thisColor.G == color.G
                && thisColor.B == color.B;
        }
    }
}
